import { Transaction } from "tiny-entity2";
import { IQueryObject } from "tiny-entity2/queryObject";
import { ServiceException } from "../common/excpetion";
import { ApiDataContext } from "../domain/apiDataContext";
import { AccountPermits } from "../domain/models/accountPermits";
import { PermitConfig } from "../domain/permitsConfig";
import { BaseService } from "./baseService";

export class AccountPermitsService extends BaseService {
    async CreateAccountPermits(entity: AccountPermits[], ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            return ctx.CreateBatch(entity.map(x => new AccountPermits().ConverToEntity<AccountPermits>(x)));
        });
    }
    async UpdateAccountPermits(entity: AccountPermits, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new AccountPermits();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    async DeleteAccountPermits(entity: AccountPermits, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            return ctx.Delete<AccountPermits>(x => x.authObjectId == $args1, ctx.AccountPermits, { $args1: entity.authObjectId });
        });
    }

    async DeleteAccountPermitsByAuthObjectId(authObjectId: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            return ctx.Delete<AccountPermits>(x => x.authObjectId == $args1, ctx.AccountPermits, { $args1: authObjectId });
        });
    }

    async GetAccountPermitsById(id: string) {
        let ctx = new ApiDataContext();
        return ctx.AccountPermits.First(x => x.id == $args1, { $args1: id });
    }
    async QueryAccountPermits(params: {
        permitId?: string;
        authObjectId?: string;
        authObjectType?: string;
        orgId?: string;
        refObjectType?: string;
        refObjectId?: string;
    }) {
        let ctx = new ApiDataContext();
        let q = ctx.AccountPermits as IQueryObject<AccountPermits>;
        if (params) {
            if (params.permitId) {
                q = q.Where(x => x.permitId == $args1, { $args1: params.permitId });
            }
            if (params.authObjectId) {
                q = q.Where(x => x.authObjectId == $args1, { $args1: params.authObjectId });
            }
            if (params.authObjectType) {
                q = q.Where(x => x.authObjectType >= $args1, { $args1: params.authObjectType });
            }
            if (params.orgId) {
                q = q.Where(x => x.orgId == $args1, { $args1: params.orgId });
            }
            if (params.refObjectType) {
                q = q.Where(x => x.refObjectType == $args1, { $args1: params.refObjectType });
            }
            if (params.refObjectId) {
                q = q.Where(x => x.refObjectId == $args1, { $args1: params.refObjectId });
            }
        }
        return q.ToList();
    }
}